$(function () {
    /**
     * ajax-post
     * 将链接转换为ajax请求，并交给handleAjax处理
     * 参数：
     * data-confirm：如果存在，则点击后发出提示。
     * 示例：<a href="xxx" class="ajax-post">Test</a>
     */
    $(document).on('click', '.ajax-post', function (e) {
        //取消默认动作，防止跳转页面
        e.preventDefault();

        //获取参数（属性）
        var url = $(this).attr('href');
        var confirmText = $(this).attr('data-confirm');

        //如果需要的话，发出确认提示信息
        if (confirmText) {
            modal_confirm(confirmText,function(){
                execute();
            });
            return false;
        }else{
            execute();
        }
        
        function execute(){
            //发送AJAX请求
            $.post(url, {}, function (a, b, c) {
                handleAjax(a);
            });
        }
    });

    $(document).on('click', '.ajax-get', function (e) {
        //取消默认动作，防止跳转页面
        e.preventDefault();
        //获取参数（属性）
        var url = $(this).attr('href');
        var confirmText = $(this).attr('data-confirm');

        //如果需要的话，发出确认提示信息
        if (confirmText) {
            modal_confirm(confirmText,function(){
                execute();
            });
            return false;
        }else{
            execute();
        }

        function execute(){
            //发送AJAX请求
            $.get(url, function (a, b, c) {
                handleAjax(a);
            });
        }
    });

    /**
     * ajax-form
     * 通过ajax提交表单，通过oneplus提示消息
     * 示例：<form class="ajax-form" method="post" action="xxx">
     */
    $(document).on('submit', 'form.ajax-form', function (e) {

        //取消默认动作，防止表单两次提交
        e.preventDefault();
        var confirmText = $(this).attr('data-confirm');
        var form = $(this);
        
        //如果需要的话，发出确认提示信息
        if (confirmText) {
            modal_confirm(confirmText,function(){
                execute();
            });
            return false;
        }else{
            execute();
        }
        
        //执行ajax
        function execute(){
            //禁用提交按钮，防止重复提交

            //var form = $(this);
            $('[type=submit]', form).addClass('disabled');

            //获取提交地址，方式
            var action = form.attr('action');
            var method = form.attr('method');
            
            //检测提交地址
            if (!action) {
                return false;
            }

            //默认提交方式为get
            if (!method) {
                method = 'get';
            }

            //获取表单内容
            var formContent = form.serialize();

            //发送提交请求
            var callable;
            if (method == 'post') {
                callable = $.post;
            } else {
                callable = $.get;
            }
            callable(action, formContent, function (a) {
                handleAjax(a);
                $('[type=submit]', form).removeClass('disabled');
            });
        }
        
        //返回
        return false;
    });
    
});

/**
 * 模态提示确认操作
 *
 * @return     {boolean}  { description_of_the_return_value }
 */
function modal_confirm(confirmText,callback,icon){
    if(confirmText == ''){
        confirmText = '确认执行该操作？'
    }
    icon = icon || 'icon-frown';
    // 自定义模态框样式
    var custom = '';
        custom += '<div class="tip-model-content">';
        custom += '<div class="tip-modal-icon text-center"><i class="icon '+ icon +' icon-5x"></i></div>';
        custom += '<p class="tip-modal-text">'+ confirmText +'</p>';
        custom += '<div class="tip-modal-btn">';
        custom += '<button type="button" class="btn" data-dismiss="modal">取消</button>';
        custom += '<button class="btn btn-primary confirm" type="button">确认</button>';
        custom += '</div>';
        custom += '</div>';


    /* 使用触发器对象直接显示 */
    (new $.zui.ModalTrigger({
        title: confirmText,
        custom: custom,
    })).show();

    $('.tip-model-content').on('click','.confirm',function(){
        callback();
        //关闭模态框
        $('[data-dismiss="modal"]').click();
    })
    return false;
}
/**
 * 绑定回到顶部
 */

$(function () {
    $(window).on('scroll', function () {
        var st = $(document).scrollTop();
        if (st > 0) {
            $('#go-top').css('display','block');
        } else {
            $('#go-top').hide();
        }
    });
    $('#tool .go-top').on('click', function () {
        $('html,body').animate({'scrollTop': 0}, 500);
    });

    $('#go-top .uc-2vm').hover(function () {
        $('#go-top .uc-2vm-pop').removeClass('dn');
    }, function () {
        $('#go-top .uc-2vm-pop').addClass('dn');
    });
});

/**
 * 绑定登出事件
 */
$(function(){
    $('[event-node=logout]').click(function () {
        $.get(Url('Ucenter/Member/logout'), function (msg) {
            toast.success(msg.msg + '。', '温馨提示');
            setTimeout(function () {
                location.href = msg.url;
            }, 1500);
        }, 'json')
    });
})

var follower = {
    'bind_follow': function () {
        $('[data-role="follow"]').unbind('click')
        $('[data-role="follow"]').click(function () {
            var $this = $(this);
            var uid = $this.attr('data-follow-who');
            $.post(Url('Ucenter/Public/follow'), {uid: uid}, function (msg) {
                if (msg.status) {

                    $this.attr('class', $this.attr('data-before'));
                    $this.attr('data-role', 'unfollow');
                    $this.html('已关注');
                    follower.bind_follow();
                    toast.success(msg.info, '温馨提示');
                } else {
                    toast.error(msg.info, '温馨提示');
                }
            }, 'json');
        })

        $('[data-role="unfollow"]').unbind('click')
        $('[data-role="unfollow"]').click(function () {
            var $this = $(this);
            var uid = $this.attr('data-follow-who');
            $.post(Url('Ucenter/Public/unfollow'), {uid: uid}, function (msg) {
                if (msg.status) {
                    $this.attr('class', $this.attr('data-after'));
                    $this.attr('data-role', 'follow');
                    $this.html('关注');
                    follower.bind_follow();
                    toast.success(msg.info, '温馨提示');
                } else {
                    toast.error(msg.info, '温馨提示');
                }
            }, 'json');
        })
    }
}

follower.bind_follow();
